package com.ziyun.pop.web.modules.capital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.capital.entity.MerchantOrderAccountEntity;
import com.ziyun.pop.modules.capital.entity.SupplierApplyCashRuleEntity;
import com.ziyun.pop.modules.capital.service.MerchantOrderAccountService;
import com.ziyun.pop.modules.capital.service.SupplierApplyCashService;
import com.ziyun.pop.modules.supp.service.SellerInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 
 *
 * @author zh
 * @email 296588519@qq.com
 * @date 2018-08-03 10:21:15
 */
@RestController
@RequestMapping("capital/merchantorderaccount")
public class MerchantOrderAccountController {
	@Autowired
	private MerchantOrderAccountService merchantOrderAccountService;


	@Autowired
	private SupplierApplyCashService supplierApplyCashService;

	@Autowired
	private SellerInfoService sellerInfoService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	/*@RequiresPermissions("capital:merchantorderaccount:list")*/
	public R list(@RequestBody Map<String, Object> params) {
		SysUserEntity user =  UserUtils.getCurentUser();
		params.put("merchantType", user.getMerchantType());// 存放用户类型
		params.put("sellerId", user.getSellerId());// 存放用户id
		PageUtils page =  merchantOrderAccountService.queryPage(params);
		SupplierApplyCashRuleEntity applyCashRule = this.supplierApplyCashService.getOne(new QueryWrapper<SupplierApplyCashRuleEntity>()
				.eq("factory_id", user.getSellerId()));
		String sellerMobile = this.sellerInfoService.getById(user.getSellerId()).getSellerMobile();
		return R.ok().put("page", page).put("applyCashRule",applyCashRule).put("sellerMobile",sellerMobile);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{orderAccountId}")
	@RequiresPermissions("capital:merchantorderaccount:info")
	public R info(@PathVariable("orderAccountId") Integer orderAccountId) {
		MerchantOrderAccountEntity merchantOrderAccount = merchantOrderAccountService.getById(orderAccountId);

		return R.ok().put("merchantOrderAccount", merchantOrderAccount);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("capital:merchantorderaccount:save")
	public R save(@RequestBody MerchantOrderAccountEntity merchantOrderAccount) {
		merchantOrderAccountService.save(merchantOrderAccount);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("capital:merchantorderaccount:update")
	public R update(@RequestBody MerchantOrderAccountEntity merchantOrderAccount) {
		ValidatorUtils.validateEntity(merchantOrderAccount);
		//merchantOrderAccountService.updateAllColumnById(merchantOrderAccount);// 全部更新

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("capital:merchantorderaccount:delete")
	public R delete(@RequestBody Integer[] orderAccountIds) {
		merchantOrderAccountService.removeByIds(Arrays.asList(orderAccountIds));

		return R.ok();
	}

}
